<?php

class Admin_UserController extends Zend_Controller_Action
{

	private $table;

	public function init()
	{
		$this->table = new Default_Model_User();
	}

	public function indexAction()
	{
		$page = (int)$this->_getParam('page', 1);
		$order = $this->_getParam('order', 'username');
		$select = $this->table->select()->order($order);
		$this->view->data = Zend_Paginator::factory($select)
			->setCurrentPageNumber($page)
			->setItemCountPerPage(20);
		$this->view->order = $order;
	}

	public function editAction()
	{
		$row = $this->table->fetchRow(array(
			'user_id = ?' => (int)$this->_getParam('id'),
		));
		if (!$row)
		{
			$row = $this->table->createRow();
		}
		$form = new Admin_Form_User();
		$form->load($row);
		if (
			$this->_request->isPost() &&
			$form->isValid($this->_request->getPost())
		)
		{
			if ($form->save($row))
			{
				$this->_helper->flashMessenger('success.edit');
			}
			$this->_helper->_redirector->gotoRoute(array(
				'action' => 'index',
				'id' => null,
			));
		}
		$this->view->form = $form;
	}

	public function deleteAction()
	{
		$row = $this->table->fetchRow(array(
			'user_id = ?' => (int)$this->_getParam('id'),
		));
		if ($row)
		{
			$row->is_deleted = 1;
			if ($row->save())
			{
				$this->_helper->flashMessenger('success.delete');
			}
		}
		$this->_helper->_redirector->gotoRoute(array(
			'action' => 'index',
			'id' => null,
		));
	}

}

?>
